/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ordering.dao;

import com.ordering.dao.exceptions.NonexistentEntityException;
import com.ordering.pojo.TFoodType;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import com.ordering.pojo.TMenu;
import com.ordering.utils.ConfigIni;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author sulan
 */
public class TFoodTypeJpaController {

    public TFoodTypeJpaController() {
        emf = EntityManagerUtils.em;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TFoodType TFoodType) {
        if (TFoodType.getTMenuCollection() == null) {
            TFoodType.setTMenuCollection(new ArrayList<TMenu>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<TMenu> attachedTMenuCollection = new ArrayList<TMenu>();
            for (TMenu TMenuCollectionTMenuToAttach : TFoodType.getTMenuCollection()) {
                TMenuCollectionTMenuToAttach = em.getReference(TMenuCollectionTMenuToAttach.getClass(), TMenuCollectionTMenuToAttach.getMenuId());
                attachedTMenuCollection.add(TMenuCollectionTMenuToAttach);
            }
            TFoodType.setTMenuCollection(attachedTMenuCollection);
            em.persist(TFoodType);
            for (TMenu TMenuCollectionTMenu : TFoodType.getTMenuCollection()) {
                TFoodType oldFoodTypeOidOfTMenuCollectionTMenu = TMenuCollectionTMenu.getFoodTypeOid();
                TMenuCollectionTMenu.setFoodTypeOid(TFoodType);
                TMenuCollectionTMenu = em.merge(TMenuCollectionTMenu);
                if (oldFoodTypeOidOfTMenuCollectionTMenu != null) {
                    oldFoodTypeOidOfTMenuCollectionTMenu.getTMenuCollection().remove(TMenuCollectionTMenu);
                    oldFoodTypeOidOfTMenuCollectionTMenu = em.merge(oldFoodTypeOidOfTMenuCollectionTMenu);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TFoodType TFoodType) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TFoodType persistentTFoodType = em.find(TFoodType.class, TFoodType.getFoodTypeOid());
            Collection<TMenu> TMenuCollectionOld = persistentTFoodType.getTMenuCollection();
            Collection<TMenu> TMenuCollectionNew = TFoodType.getTMenuCollection();
            Collection<TMenu> attachedTMenuCollectionNew = new ArrayList<TMenu>();
            for (TMenu TMenuCollectionNewTMenuToAttach : TMenuCollectionNew) {
                TMenuCollectionNewTMenuToAttach = em.getReference(TMenuCollectionNewTMenuToAttach.getClass(), TMenuCollectionNewTMenuToAttach.getMenuId());
                attachedTMenuCollectionNew.add(TMenuCollectionNewTMenuToAttach);
            }
            TMenuCollectionNew = attachedTMenuCollectionNew;
            TFoodType.setTMenuCollection(TMenuCollectionNew);
            TFoodType = em.merge(TFoodType);
            for (TMenu TMenuCollectionOldTMenu : TMenuCollectionOld) {
                if (!TMenuCollectionNew.contains(TMenuCollectionOldTMenu)) {
                    TMenuCollectionOldTMenu.setFoodTypeOid(null);
                    TMenuCollectionOldTMenu = em.merge(TMenuCollectionOldTMenu);
                }
            }
            for (TMenu TMenuCollectionNewTMenu : TMenuCollectionNew) {
                if (!TMenuCollectionOld.contains(TMenuCollectionNewTMenu)) {
                    TFoodType oldFoodTypeOidOfTMenuCollectionNewTMenu = TMenuCollectionNewTMenu.getFoodTypeOid();
                    TMenuCollectionNewTMenu.setFoodTypeOid(TFoodType);
                    TMenuCollectionNewTMenu = em.merge(TMenuCollectionNewTMenu);
                    if (oldFoodTypeOidOfTMenuCollectionNewTMenu != null && !oldFoodTypeOidOfTMenuCollectionNewTMenu.equals(TFoodType)) {
                        oldFoodTypeOidOfTMenuCollectionNewTMenu.getTMenuCollection().remove(TMenuCollectionNewTMenu);
                        oldFoodTypeOidOfTMenuCollectionNewTMenu = em.merge(oldFoodTypeOidOfTMenuCollectionNewTMenu);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = TFoodType.getFoodTypeOid();
                if (findTFoodType(id) == null) {
                    throw new NonexistentEntityException("The tFoodType with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TFoodType TFoodType;
            try {
                TFoodType = em.getReference(TFoodType.class, id);
                TFoodType.getFoodTypeOid();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The TFoodType with id " + id + " no longer exists.", enfe);
            }
            Collection<TMenu> TMenuCollection = TFoodType.getTMenuCollection();
            for (TMenu TMenuCollectionTMenu : TMenuCollection) {
                TMenuCollectionTMenu.setFoodTypeOid(null);
                TMenuCollectionTMenu = em.merge(TMenuCollectionTMenu);
            }
            em.remove(TFoodType);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TFoodType> findTFoodTypeEntities() {
        return findTFoodTypeEntities(true, -1, -1);
    }

    public List<TFoodType> findTFoodTypeEntities(int maxResults, int firstResult) {
        return findTFoodTypeEntities(false, maxResults, firstResult);
    }

    private List<TFoodType> findTFoodTypeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from TFoodType as o order by o.foodTypeName");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TFoodType findTFoodType(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TFoodType.class, id);
        } finally {
            em.close();
        }
    }

    public int getTFoodTypeCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from TFoodType as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
